package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.constant.Constants;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.AlertRuleDto;
import com.zzyl.entity.AlertData;
import com.zzyl.entity.AlertRule;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.AlertRuleMapper;
import com.zzyl.service.AlertRuleService;
import com.zzyl.vo.AlertRuleVo;
import com.zzyl.vo.DeviceDataVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author sjqn
 */
@Slf4j
@Service
public class AlertRuleServiceImpl implements AlertRuleService {

    @Autowired
    private AlertRuleMapper alertRuleMapper;

    /**
     * 新增报警规则
     * @param dto
     * @return
     */
    @Override
    public ResponseResult createAlertRule(AlertData dto) {
        AlertRule alertRule = BeanUtil.toBean(dto, AlertRule.class);
        alertRuleMapper.insertAlertRule(alertRule);
        return ResponseResult.success();
    }

    //5.2 分页获取告警规则列表
    @Override
    public ResponseResult<PageResponse<AlertRuleVo>> getPage(Integer pageNum,
                                               Integer pageSize,
                                               String alertRuleName,
                                               String functionName,
                                               String productKey) {
        PageHelper.startPage(pageNum,pageSize);
       Page<AlertRuleVo> page = alertRuleMapper.getPage(alertRuleName,functionName,productKey);
        PageResponse<AlertRuleVo> pageResponse = PageResponse.of(page, AlertRuleVo.class);
        return ResponseResult.success(pageResponse);
    }

    /**
     * 获取单个告警规则
     * @param id
     * @return
     */
    @Override
    public ResponseResult<AlertRuleVo> readById(Long id) {
        return ResponseResult.success(alertRuleMapper.selectAlertRuleById(id));
    }

    /**
     * 更新告警规则
     * @param id
     * @return
     */
    @Override
    public ResponseResult updateAlertRule(Long id,AlertRuleDto dto) {
        AlertRule alertRule = BeanUtil.toBean(dto, AlertRule.class);
        alertRuleMapper.updateAlertRule(id,alertRule);
        return ResponseResult.success();
    }

    /**
     *  删除告警规则
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteAlertRule(Long id) {
        //在禁用状态下，不能删除
        AlertRule alertRule = alertRuleMapper.selectAlertRuleById(id);
        if (SuperConstant.DATA_STATE_1.equals(alertRule.getStatus())){
            throw new BaseException(BasicEnum.ENABLED_CANNOT_DELETED);
        }
        alertRuleMapper.deleteAlertRule(id);
        return ResponseResult.success();
    }

    /**
     * 启用-禁用
     * @param id
     * @param status
     * @return
     */
    @Override
    public ResponseResult startOrStop(Long id, Long status) {
        AlertRule alertRule = new AlertRule();
        alertRule.setStatus(Integer.parseInt(status.toString()));
        alertRuleMapper.updateAlertRule(id,alertRule);
        return ResponseResult.success();
    }
}
